package com.cloudbench.util;

import static com.cloudbench.common.Constants.CONNECTION_TIMEOUT;
import static com.cloudbench.common.Constants.SOCKET_TIMEOUT;
//import static com.cloudbench.common.ConstantsKey.KEY_HTTP_SESSION_HEADER;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.content.Context;
import ch.boye.httpclientandroidlib.HttpHost;
import ch.boye.httpclientandroidlib.HttpVersion;
import ch.boye.httpclientandroidlib.NameValuePair;
import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.client.entity.UrlEncodedFormEntity;
import ch.boye.httpclientandroidlib.client.methods.HttpGet;
import ch.boye.httpclientandroidlib.client.methods.HttpPost;
import ch.boye.httpclientandroidlib.conn.routing.HttpRoute;
import ch.boye.httpclientandroidlib.conn.scheme.PlainSocketFactory;
import ch.boye.httpclientandroidlib.conn.scheme.Scheme;
import ch.boye.httpclientandroidlib.conn.scheme.SchemeRegistry;
import ch.boye.httpclientandroidlib.conn.ssl.SSLSocketFactory;
import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;
import ch.boye.httpclientandroidlib.impl.conn.PoolingClientConnectionManager;
import ch.boye.httpclientandroidlib.params.BasicHttpParams;
import ch.boye.httpclientandroidlib.params.HttpConnectionParams;
import ch.boye.httpclientandroidlib.params.HttpParams;
import ch.boye.httpclientandroidlib.params.HttpProtocolParams;
import ch.boye.httpclientandroidlib.protocol.HTTP;

/**
 * HttpClient utility's factory class
 * 
 * @author justin@zerodesktop.com
 * @since 2011/08/16
 * @version 1.0.0
 *
 */
public class WebClientFactory {
	static String CNAME = WebClientFactory.class.getSimpleName();
	
	/**
	 * 
	 * @param url
	 * @param sid
	 * @return
	 * @throws IOException
	 */
	public static HttpGet createHttpGet(String url, String sid) throws IllegalArgumentException {		
		HttpGet get = new HttpGet(url);
//		get.addHeader(KEY_HTTP_SESSION_HEADER, sid);	

		return get;
	}
	
	public static HttpPost createHttpPost(String url, String sid) throws IllegalArgumentException {
		HttpPost post = new HttpPost(url);
		
//		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		post.setHeader("Content-Type", "application/json");
//		post.addHeader(KEY_HTTP_SESSION_HEADER, sid);
			
		return post;
	}

	/**
	 * for login
	 * @param url
	 * @param params
	 * @return
	 * @throws IllegalArgumentException
	 * @throws UnsupportedEncodingException
	 */
	public static HttpPost createHttpPost(String url, ArrayList<NameValuePair> params) throws IllegalArgumentException,
			UnsupportedEncodingException {
		HttpPost post = new HttpPost(url);
		post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		
	    
		return post;
	}

	public static HttpPost createHttpPost(String url, String sid, ArrayList<NameValuePair> params) throws IllegalArgumentException,
			UnsupportedEncodingException {
		HttpPost post = new HttpPost(url);
//		post.addHeader(KEY_HTTP_SESSION_HEADER, sid);
		post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		post.setHeader("Content-Type", "application/json");
//		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		
			
		
		//UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
        //entity.setContentType("application/x-www-form-urlencoded");
        //post.setEntity(entity);
        
		return post;
	}

	public static HttpPost createHttpPost(String url, String sid, ArrayList<NameValuePair> params, boolean urlEncoded) throws IllegalArgumentException, UnsupportedEncodingException {
		HttpPost post = new HttpPost(url);
//		post.addHeader(KEY_HTTP_SESSION_HEADER, sid);
		post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		if(urlEncoded) {
			post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		} else {
			post.setHeader("Content-Type", "application/json");	
		}

		return post;
	}

	
	public static HttpClient createHttpClient(Context c) throws Exception {
		//return createHttpClient(CONNECTION_TIMEOUT, SOCKET_TIMEOUT);
		return createHttpClient(c, CONNECTION_TIMEOUT, SOCKET_TIMEOUT);
	}
	
	public static HttpClient createHttpClient(Context c, int readTimeout) throws Exception {
		return createHttpClient(c, CONNECTION_TIMEOUT, readTimeout);
	}
	
	public static HttpClient createHttpClient(Context c, int cTimeout, int sTimeout) throws Exception {
		// try {
//		KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//		trustStore.load(null, null);
//
//		SSLSocketFactory sslFactory = new EasySSLSocketFactory(trustStore);
//		sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

		HttpConnectionParams.setConnectionTimeout(params, cTimeout);
		HttpConnectionParams.setSoTimeout(params, sTimeout);
		HttpConnectionParams.setSocketBufferSize(params, 2 * 8192);

		HttpProtocolParams.setUseExpectContinue(params, true);
		
//		SchemeRegistry registry = new SchemeRegistry();
//		
//		registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
//		registry.register(new Scheme("https", sslFactory, 443));
		
//		return new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
		
		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(
		         new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
		schemeRegistry.register(
		         new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

		PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
		// Increase max total connection to 200
		cm.setMaxTotal(200);
		// Increase default max connection per route to 20
		cm.setDefaultMaxPerRoute(20);
		// Increase max connections for localhost:80 to 50
//		HttpHost localhost = new HttpHost("locahost", 80);
//		cm.setMaxPerRoute(new HttpRoute(localhost), 50);
		 
		HttpClient httpClient = new DefaultHttpClient(cm, params);
		
		return httpClient;
		
	}
	
	
}